package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginStudentServlet")
public class loginStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //对用户名进行判断,看是否存在,如果不存在就让他通过重定向跳回登录界面去
        if (username==null||"".equals(username)){
            response.sendRedirect(getServletContext().getContextPath()+"/login.jsp");
            return;
        }
        //用户名不为空的话,就把用户名存放到会话域中,在一个用户可能存在常见就浏览网站的前提下,存放在会话域是最合适的
        request.getSession().setAttribute("username",username);
        request.getSession().setAttribute("password",password);
        //接着将用户重定向到首页去,这个时候,用户就已经完成了登录的行为了,
        //重定向首页的时候,就会直接及进行是否登录的判定,也就会直接显示任务选项
        response.sendRedirect(getServletContext().getContextPath()+"/index.jsp");
    }
}
